package user

import java.util.Date;
import report.Activity 

class User {
	
	static mapping = {
        tablePerHierarchy false
    }

	String nome
	String sobrenome
	String email
	String login
	String password
	Date admission
	boolean isAdmin
	
	static hasMany = [activities:Activity]	
		
	static constraints = {
		nome(blank:false, nullable:false, size:2..128, matches:"(\\p{L})+(\\s(\\p{L})*)*")
		sobrenome(blank:false, nullable:false, size:2..128, matches:"(\\p{L})+(\\s(\\p{L})*)*")
		login(blank:false, nullable: false,  size:4..15, unique:true, matches:"(\\w)*")
		password(blank:false, password:true,size:5..100)
		email(email:true, blank:false, nullable:false, unique:true)
		admission(nullable:false, max: new Date())
		isAdmin(nullable:false)
	}
	
	static def authenticate(login, password) {
		def user = User.findByLogin(login)
		
		if (user?.password == password.encodeAsHash()) {
			return user
		}
		
		return null
	}
	
	def boolean equals(obj) {
		if (obj instanceof User) {
			User u = (User) obj
			return this.login == u.login
		}
		return false
   }	
}
